package itemCF.step5;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author legolas
 * @date 2020/3/13 下午7:06
 */

public class Mapper5 extends Mapper<LongWritable, Text, Text, Text> {

    private List<String> cacheList = new ArrayList<>();
    private DecimalFormat df = new DecimalFormat("0.00");

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        super.setup(context);
        try {
            //步骤1的评分矩阵
            FileReader fr = new FileReader("itemUserScore5");
            BufferedReader br = new BufferedReader(fr);
            String line = null;
            while ((line = br.readLine()) != null) {
                cacheList.add(line);
            }
            fr.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 需要实现的map函数
     * 这个map函数就是可以接收k1，v1   产生k2，v2
     *
     * @param k1
     * @param v1
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */


    /**
     * 根据评分矩阵，将步骤4输出中用户已有行为置0
     * 1    A_9.9    B_2.4     C_23.9
     * 2    A_16.6    B_8.3
     */

    @Override
    protected void map(LongWritable k1, Text v1, Context context) throws IOException, InterruptedException {
        Text k2 = new Text();
        Text v2 = new Text();
        try {
            //行:物品id
            String row_matix4 = v1.toString().split("\t")[0];
            String[] value_array_matix4 = v1.toString().split("\t")[1].split(",");

            for (String cache_row : cacheList) {
                String row_matrix1 = cache_row.split("\t")[0];
                String[] value_array_matrix1 = cache_row.split("\t")[1].split(",");

                //如果物品id相同
                if (row_matrix1.equals(row_matix4)) {
                    for (String value_matrix4 : value_array_matix4) {
                        boolean flag = true;
                        String user_id4 = value_matrix4.split("_")[0];
                        String score4 = value_matrix4.split("_")[1];

                        //相同物品id的行与matrix1中，相同用户id的列比较
                        for (String value_matrix1 : value_array_matrix1) {
                            String user_id1 = value_matrix1.split("_")[0];
                            if (user_id1.equals(user_id4)) {
                                flag = false;
                            }
                        }
                        if (flag == true) {
                            k2.set(row_matix4);
                            v2.set(user_id4 + "_" + score4);
                            context.write(k2, v2);
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
